package com.zk.wx.controller;

import com.zk.wx.service.CoreService;
import com.zk.wx.util.SignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 15911 on 2017/10/15.
 */
@RestController
@RequestMapping
public class CheckTokenController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/wx")
    public String checkToken(@RequestParam("signature") String signature,
                             @RequestParam("timestamp") String timestamp,
                             @RequestParam("nonce") String nonce,
                             @RequestParam("echostr") String echostr){
        logger.info("checkToken====>param===>signature=" + signature
                + "||timestamp=" + timestamp
                + "||nonce=" + nonce
                + "||echostr=" + echostr);
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        boolean b = SignUtil.checkSignature(signature, timestamp, nonce);
        logger.info("checkToken服务器签名校验结果：" + b);
        if (b) {
            logger.info("checkToken服务器验证成功！！！！！");
            return echostr;
        }
        logger.info("checkToken服务器验证失败！！！");
        return null;
    }
    @PostMapping("/wx")
    public String Handle(HttpServletRequest request){
        logger.info("wx-server handle===>start");
        // 消息的接收、处理、响应
        // 调用核心业务类接收消息、处理消息
        String respXml = CoreService.processRequest(request);
        return respXml;
    }
}
